﻿///图片等比例缩放显示方法
///ImgD为图片的jquery对象，iwidth显示的最大宽度，iheight显示的最大高度，isleft是为了判断图片是否要居左，如果有值则不居左
///举例：使用方式为var img=$("#id");img.ready(function(){resizeimg(img,90,90);});
function resizeimg(ImgD, iwidth, iheight,isleft) {
    var image = new Image();
    image.src = ImgD.attr("src");
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.css({ "width": iwidth + "px", "height": (image.height * iwidth) / image.width + "px" });
            } else {
                ImgD.css({ "width": image.width + "px", "height": image.height + "px" });
            }
        }
        else {
            if (image.height > iheight) {
                ImgD.css({ "width": (image.width * iheight) / image.height + "px", "height": iheight + "px" });
            } else {
                ImgD.css({ "width": image.width + "px", "height": image.height + "px" });
            }
        }
    }
    if (isleft == undefined) {
        ImgD.css({ "display": "block", "margin-left": (iwidth - ImgD.width()) / 2, "margin-top": (iheight - ImgD.height()) / 2 });
    }
    else if (isleft) {
        ImgD.css({ "display": "block", "margin-top": (iheight - ImgD.height()) / 2 });
    }
    else {
        ImgD.css({ "display": "block" });
    }
}